/**
 * @author GreaseMonkey
 */
function RFConfig(configFile) {
  this.onload = null;
  this.plugins = null;
  this.configFile = configFile;

  this.find = function(namespace, name) {
    namespace = namespace.toLowerCase();
    name = name.toLowerCase();

    for (var i = 0, plugin = null; (plugin = this.plugins[i]); i++) {
      if (plugin.namespace.toLowerCase() == namespace && plugin.name.toLowerCase() == name) {
        return i;
      }
    }

    return -1;
  }
  
  this.initFilename = function(plugin) {
    var index = {};
    var base = plugin.name.replace(/[^A-Z0-9_]/gi, "").toLowerCase();

    // If no Latin characters found - use default
    if (!base) {
      base = "rf_plugin";
    }

    // 24 is a totally arbitrary max length
    if (base.length > 24) {
      base = base.substring(0, 24);
    }
    
    for (var i = 0; i < this.plugins.length; i++) {
      index[this.plugins[i].filename] = this.plugins[i];
    }
    
    if (!index[base + ".rainfox.js"]) {
      plugin.filename = base + ".rainfox.js";
      return;
    }
    
    for (var count = 1; count < Number.MAX_VALUE; count++) {
      if (!index[base + count + ".rainfox.js"]) {
        plugin.filename = base + count + ".rainfox.js";
        return;
      }
    }
    
	// TODO: Qué es esto?
    throw new Error("doooooooode. get some different user script or something.");
  }
  
  this.load = function() {
    var domParser = Components.classes["@mozilla.org/xmlextras/domparser;1"]
                              .createInstance(Components.interfaces.nsIDOMParser);
	this.plugins = [];
	try {
	    var configContents = RF_IO.getContents(RF_PLUGIN.getPluginFileURI("config.xml"));
	    var doc = domParser.parseFromString(configContents, "text/xml");
	    var nodes = doc.evaluate("/PluginsConfig/Plugin", doc, null, 0, null);
		
		//alert(nodes);
	
	    //this.scripts = [];
	
	    for (var node = null; (node = nodes.iterateNext()); ) {
	      var plugin = new PluginInfo();
	
	/*
	      for (var i = 0, childNode = null; (childNode = node.childNodes[i]); i++) {
	        if (childNode.nodeName == "Include") {
	          script.includes.push(childNode.firstChild.nodeValue);
	        }
	        else if (childNode.nodeName == "Exclude") {
	          script.excludes.push(childNode.firstChild.nodeValue);
	        }
	      }
	*/
	      plugin.filename = node.getAttribute("filename");
	      plugin.name = node.getAttribute("name");
	      plugin.namespace = node.getAttribute("namespace");
	      plugin.description = node.getAttribute("description");
	      plugin.enabled = node.getAttribute("enabled") == true.toString();
	
	      this.plugins.push(plugin);
	    }
	} catch(e) {
	//	alert(e);
	}
  }

  this.save = function() {
    var doc = document.implementation.createDocument("", "PluginsConfig", null);
    
    for (var i = 0, pluginObj = null; (pluginObj = this.plugins[i]); i++) {
      var pluginNode = doc.createElement("Plugin");

/*
      for (var j = 0; j < scriptObj.includes.length; j++) {
        var includeNode = doc.createElement("Include");
        includeNode.appendChild(doc.createTextNode(scriptObj.includes[j]));
        scriptNode.appendChild(doc.createTextNode("\n\t\t"));
        scriptNode.appendChild(includeNode);
      }

      for (var j = 0; j < scriptObj.excludes.length; j++) {
        var excludeNode = doc.createElement("Exclude");
        excludeNode.appendChild(doc.createTextNode(scriptObj.excludes[j]));
        scriptNode.appendChild(doc.createTextNode("\n\t\t"));
        scriptNode.appendChild(excludeNode);
      }
*/
      pluginNode.appendChild(doc.createTextNode("\n\t"));

      pluginNode.setAttribute("id", pluginObj.id);
      pluginNode.setAttribute("author", pluginObj.author);
      pluginNode.setAttribute("name", pluginObj.name);
      pluginNode.setAttribute("filename", pluginObj.filename);
      pluginNode.setAttribute("url", pluginObj.url);
      pluginNode.setAttribute("version", pluginObj.version);
      pluginNode.setAttribute("namespace", pluginObj.namespace);
      pluginNode.setAttribute("license", pluginObj.license);
      pluginNode.setAttribute("description", pluginObj.description);
      pluginNode.setAttribute("enabled", pluginObj.enabled);

      doc.firstChild.appendChild(doc.createTextNode("\n\t"));
      doc.firstChild.appendChild(pluginNode);
    }

    doc.firstChild.appendChild(doc.createTextNode("\n"))

    var configStream = RF_IO.getWriteStream(this.configFile);
    new XMLSerializer().serializeToStream(doc, configStream, "utf-8");
    configStream.close();
  }
}

function PluginInfo() {
	this.id			= null;	// id: manolo@www.manolo.com
	this.author		= null;	// autor del plugin: manolo
	this.name		= null;	// nombre del plugin: Manolo
	this.filename	= null;	// nombre del fichero: manolo.rainfox.js
	this.url		= null;	// url al plugin: http://www.manolo.com/plugins/manolo.rainfox.js
	this.version	= null;	// versión del plugin: 1.0
	this.namespace	= null;	// web del plugin: http://www.manolo.com/mi-plugin/
	this.license	= 'GPL';// licencia bajo la que se distribuye: GPL
	this.description= null;	// descripción de lo que busca el plugin: artículos en el blog de manolo
	this.enabled	= true;	// si está activo o no
}; 
